#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
//#include<stdlib.h>
//
//typedef struct Su
//{
//	char name[20];
//	int age;
//	float sroce;
//}Student;
//
//int compare(const void* e1, const void* e2)
//{
//	if (((Student*)e1)->sroce > ((Student*)e2)->sroce)
//	{
//		return 1;
//	}
//	else if (((Student*)e1)->sroce < ((Student*)e2)->sroce)
//	{
//		return -1;
//	}
//	else
//		return 0;
//	
//}
//
//int main(void)
//{
//	Student arr[4] = { {"liyi",10,12.6f},{"wangwu",20,13.5f},{"zhangsan",30,45.6f},{"bibi",40,89.3}};
//	int sz = sizeof(arr) / sizeof(arr[0]);
//	qsort(arr, sz, sizeof(arr[1]), compare);
//	for (int i = 0; i < sz; i++)
//	{
//		printf("%s %d %.2f\n", arr[i].name, arr[i].age, arr[i].sroce);
//	}
//	return 0;
//}


#include<stdlib.h>

typedef struct Su
{
	char name[20];
	int age;
	float sroce;
}Student;

int compare(const void* e1, const void* e2)
{
	if (((Student*)e1)->sroce > ((Student*)e2)->sroce)
	{
		return 1;
	}
	else if (((Student*)e1)->sroce < ((Student*)e2)->sroce)
	{
		return -1;
	}
	else
		return 0;

}

void swap(char* buf1, char* buf2,int width)
{
	for (int i = 0; i < width; i++)
	{
		char temp = *buf1;
		*buf1 = *buf2;
		*buf2 = temp;
		buf1++;
		buf2++;
	}
	
}

void my_qsort(void* base, int sz, int width, int (*cmp)(const void*, const void*))
{
	int i = 0;
	int j = 0;
	for (i = 0; i < sz - 1; i++)
	{
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base+j* width, (char*)base + (j+1)*width)>0)
			{
				swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
			}
		}
	}
}

int main(void)
{
	Student arr[4] = { {"liyi",10,180.6f},{"wangwu",20,1.5f},{"zhangsan",30,45.6f},{"bibi",40,9.3} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	my_qsort(arr, sz, sizeof(arr[1]), compare);
	for (int i = 0; i < sz; i++)
	{
		printf("%s %d %.2f\n", arr[i].name, arr[i].age, arr[i].sroce);
	}
	return 0;
}

